package com.mrzhou.auth.utils;

import com.mrzhou.core.tool.constants.CharsetsConstant;
import com.mrzhou.core.tool.constants.StringSymbol;
import com.mrzhou.core.tool.constants.TokenConstant;
import com.mrzhou.web.util.WebUtil;
import lombok.SneakyThrows;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;

import java.util.Base64;

/**
 * token工具类
 */
public class TokenUtil {

    public final static String CLIENT_ID = TokenConstant.CLIENT_ID;
    public final static String USER_ID = TokenConstant.USER_ID;
    public final static String DEPT_ID = TokenConstant.DEPT_ID;
    public final static String ROLE_ID = TokenConstant.ROLE_ID;
    public final static String ROLE_NAME = TokenConstant.ROLE_NAME;
    public final static String ACCOUNT = TokenConstant.ACCOUNT;
    public final static String NAME = TokenConstant.NAME;
    public final static String REAL_NAME = TokenConstant.REAL_NAME;
    public final static String AVATAR = TokenConstant.AVATAR;
    public final static String PHONE = TokenConstant.PHONE;
    public final static String EMAIL = TokenConstant.EMAIL;
    public final static String CLIENT_SECRET = TokenConstant.CLIENT_SECRET;
    public final static String HEADER_TENANT_ID = TokenConstant.TENANT_ID_HEADER;
    public final static String HEADER_NAME_AUTHORIZATION = "Authorization";
    public final static String HEADER_PREFIX = "Basic ";
    public final static String DEFAULT_AVATAR = TokenConstant.DEFAULT_AVATAR;

    public final static String USERNAME_PARAM = "username";
    public final static String TENANT_ID_PARAM = TokenConstant.TENANT_ID_PARAM;
    public final static String AUTH_TYPE_PARAM = "auth_type";
    public final static String GRANT_TYPE_PARAM = "grant_type";
    public final static String CLIENT_IP_PARAM = "client_ip";
    public final static String SMS_CODE_PARAM = "sms_code";
    // 是否需要返回授权码标识
    public final static String FLAG_CODE_PARAM = "flag_code";
    public final static String ERROR_PARAM = "message";
    public final static String ERROR_HANDLER_URL = "/auth/error";
    public final static String AUTH_PASSWORD = "password";
    public final static String AUTH_REFRESH_TOKEN = "refresh_token";
    // 密码登录
    public final static String AUTH_PSW = "auth_psw";
    // 二维码登录
    public final static String AUTH_QR = "auth_qr";
    // 授权码登录
    public final static String AUTH_CODE = "auth_code";
    // 邮箱或手机验证码登录
    public final static String AUTH_SMS = "auth_sms";

    /**
     * 获取请求头中的client_id
     */
    public static String getClientIdFromHeader(){
        String[] info = extractAndDecodeHeader();
        return info[0];
    }

    /**
     * 解码
     */
    @SneakyThrows
    public static String[] extractAndDecodeHeader(){
        String header = WebUtil.getRequest().getHeader(TokenUtil.HEADER_NAME_AUTHORIZATION);
        if (header == null || !header.startsWith(TokenUtil.HEADER_PREFIX)) {
            throw new UnapprovedClientAuthenticationException("请求头中无client信息");
        }
        byte[] base64Token = header.substring(6).getBytes(CharsetsConstant.UTF_8_NAME);

        byte[] decoded;
        try{
            decoded = Base64.getDecoder().decode(base64Token);
        } catch (IllegalArgumentException e) {
            throw new BadCredentialsException("Failed to decode basic authentication token");
        }

        String token = new String(decoded, CharsetsConstant.UTF_8_NAME);
        int index = token.indexOf(StringSymbol.COLON);
        if (index == -1) {
            throw new BadCredentialsException("Invalid basic authentication token");
        } else {
            return new String[]{token.substring(0, index), token.substring(index + 1)};
        }
    }




}
